package com.herench.bs.handler;

import com.herench.bs.agent.IOutpatientClinicAgent;
import com.herench.bs.annotation.Handler;
import com.herench.bs.support.AbstractMessageHandler;
import com.herench.bs.dao.IClinicAppointmentRecordDao;
import com.herench.bs.support.RequestMessage;
import com.herench.bs.support.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.Map;

/**
 * com.herench.bs.handler
 *
 * @author wangwenkai
 * @date 2017/10/24/15:54
 * E-mail：605064334@qq.com
 */
@Component
@Handler(code = "100740" ,name="当日挂号交易撤销")
public class CanceledRegisterInsuranceTransactionHandler extends AbstractMessageHandler {
    @Autowired
    private IClinicAppointmentRecordDao iClinicAppointmentRecordDao;

    @Override
    protected ResponseMessage innerHandler(RequestMessage requestMessage) throws Exception {
        ResponseMessage responseMessage = new ResponseMessage(requestMessage.getId());
        String channelCode = requestMessage.getArgsPropertyStringValue("sysCode");
        String recordId = requestMessage.getArgsPropertyStringValue("recordId");
        Assert.notNull(recordId, "recordId 不可为空！");
        Map<String, Object> res = new HashMap<>(16);
        Map<String, Object> params = new HashMap<>(16);
        params.put("id", recordId);
        Map<String, Object> resFindOne = iClinicAppointmentRecordDao.findOne(params);
        if (resFindOne != null) {
            String idCard = (String) resFindOne.get("idCard");
            IOutpatientClinicAgent outpatientClinicAgent = getCleverClinicContext().getApplicationContext().getBean(IOutpatientClinicAgent.class);
            String hospitalCode = getCleverClinicContext().getHospitalCodeByChannelCode(channelCode);
            Map<String, Object> resPatientId = outpatientClinicAgent.queryPatientInfo(channelCode, hospitalCode, idCard);
            String patientId = (String) resPatientId.get("patientId");
            if (!(patientId == null || "".equals(patientId))) {
                res = outpatientClinicAgent.canceleRegisterTransactionInsurance(channelCode, hospitalCode, recordId, patientId);
            } else {
                res = resPatientId;
            }
        } else {
            res.put(ResponseMessage.RES_CODE, "-3");
            res.put(ResponseMessage.MESSAGE, "未找到相关记录");
            res.put(ResponseMessage.MESSAGE_IN, "未找到相关记录");
            res.put(ResponseMessage.MESSAGE_OUT, "未找到相关记录");
        }
        responseMessage.setData(res);
        getPropertiesFromInvokerResultMap(responseMessage, res);
        return responseMessage;
    }
}
